package com.lsh.controller;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/6/24 1:48 下午
 * @desc ：使用Shiro进行密码加密
 */

import com.lsh.dao.db1.UserMapper;
import com.lsh.entity.User;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MD5Controller {
    @Autowired
    UserMapper userMapper;

    /**
     * 使用Shiro的MD5进行密码加密    不可逆
     * @param user
     * @return
     */
    @GetMapping("/addUser1")
    public String  md5(User user){
        String pwd = user.getPassword();
        String salt = "lsh";
        //Md5Hash() 要加密的密码,   盐值,   循环加密次数
        String password = new Md5Hash(pwd, salt, 10).toString();
        user.setPassword(password);
        userMapper.addUser(user);
        return "添加成功";
    }

    /**
     * 使用Shiro的Base64进行编码   可逆
     * @param user
     * @return
     */
    @GetMapping("/addUser2")
    public String  baes64(User user){
        String pwd = user.getPassword();
        String salt = "lsh";
        //Md5Hash() 要加密的密码,   盐值,   循环加密次数
        String password = Base64.encodeToString(pwd.getBytes());
        String decode = Base64.decodeToString(password);
        System.out.println("Base64解码后:"+decode);
        user.setPassword(password);
        userMapper.addUser(user);
        return "添加成功";
    }


}
